package routes

import (
	"fmt"
	"net/http"
	"strings"

	"gitee.com/anxu/golang-web/src/service"
)

const static = "static"

func Auth(next http.Handler) http.Handler {

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		if strings.HasPrefix(r.URL.Path, fmt.Sprintf("/%s/", static)) {
			next.ServeHTTP(w, r)
		} else {
			middleware(next, w, r)
		}
	})
}

func middleware(next http.Handler, w http.ResponseWriter, r *http.Request) {
	service.Session.Start(r, w)
	service.User.LoginByCookie(w, r)
	// Call the next handler, which can be another middleware in the chain, or the final handler.
	next.ServeHTTP(w, r)
}
